<template>
  <!-- Dashboard -->
  <div class="page-container">
    <el-form :inline="true" label-width="100px" label-suffix=":">
      <el-row>
        <el-col :span="6">
          <el-form-item label="信号灯编号">
            <el-input v-model="formData.lightCode" style="width: 100%" placeholder="请输入信号灯编号查询" clearable />
          </el-form-item>
        </el-col>
        <!-- 查询按钮 -->
        <el-col :span="4">
          <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
          <el-button plain icon="el-icon-refresh" @click="resetForm">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div stylle="height: calc(100vh - 160px)">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row
        style="height: 500px">
        <el-table-column align="center" label="信号灯编号" prop="lightCode"></el-table-column>
        <el-table-column align="center" label="路口ID" prop="intersectionId"></el-table-column>
        <el-table-column align="center" label="路口名称" prop="intersectionName"></el-table-column>
        <el-table-column align="center" label="信号灯型号" prop="lightModel"></el-table-column>
        <el-table-column align="center" label="控制方向" prop="controlDirection"></el-table-column>
        <el-table-column align="center" label="红灯时间(s)" prop="redLightDuration">
          <template slot-scope="scope">
            <span v-if="userType === 'NORMAL_USER'">{{ scope.row.redLightDuration }}</span>
            <el-input v-if="userType === 'MANAGER_USER'" type="number" v-model="scope.row.redLightDuration" @input="handleInput(scope.row)"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="黄灯时间(s)" prop="yellowLightDuration">
          <template slot-scope="scope">
            <span v-if="userType === 'NORMAL_USER'">{{ scope.row.yellowLightDuration }}</span>
            <el-input v-if="userType === 'MANAGER_USER'" type="number" v-model="scope.row.yellowLightDuration" @input="handleInput(scope.row)"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="绿灯时间(s)" prop="greenLightDuration">
          <template slot-scope="scope">
            <span v-if="userType === 'NORMAL_USER'">{{ scope.row.greenLightDuration }}</span>
            <el-input v-if="userType === 'MANAGER_USER'" type="number" v-model="scope.row.greenLightDuration" @input="handleInput(scope.row)"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <span class="el-icon-e" @click="$refs.mDialog.open(scope.row.id)">查看实时状态</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 20px; text-align: center">
      <!-- 页码改变和页数改变事件 -->
      <el-pagination v-show="list" background layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total"
        :current-page="pageInfo.currentPage" :page-sizes="[10, 20, 30, 40, 50]" style="float: right"
        @current-change="currentPageChange" @size-change="sizeChange" />
    </div>

    <m-dialog ref="mDialog"></m-dialog>

  </div>
</template>

<script>
import { mapGetters } from "vuex";
import mDialog from "./components/dialogIntersection.vue";
import { getTmSignalPage, updateTmTrafficLight } from "@/api/tm-road";
import { _debounce } from '@/utils/index'

export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["name", 'userType']),
  },
  components: {
    mDialog
  },
  data() {
    return {
      formData: {
        lightCode: "",
      },
      dateRange: [],
      listLoading: false,
      list: [], // 列表数据
      pageInfo: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取列表数据
    async fetchData() {
      this.listLoading = true;
      const params = {
        lightCode: this.formData.lightCode,
        pageNum: this.pageInfo.currentPage,
        pageSize: this.pageInfo.pageSize,
      }
      const { code = undefined, items = {} } = await getTmSignalPage({ ...params })
      if (code === 200) {
        this.list = items.records;
        this.pageInfo.total = items.total
        setTimeout(() => {
          this.listLoading = false
        }, 200)
      } else {
        this.$message.error('获取数据失败')
        this.listLoading = false
      }
    },
    // 更新
    async updateTmTrafficLight(datas){
      const { code = undefined, message = '' } = await updateTmTrafficLight({ ...datas })
      if (code == 200) {
        this.fetchData();
        this.$message.success('修改成功')
      } else {
        this.$message.error(message || '修改失败')
      }
    },
    handleInput: _debounce(function(val){
        this.updateTmTrafficLight(val)
    }, 800),
    // 重置表单
    resetForm() {
      this.formData = {
        lightCode: "",
      };
      this.fetchData()
    },
    // 页码改变事件
    currentPageChange(currentPage) {
      this.pageInfo.currentPage = currentPage;
      this.fetchData()
    },
    // 页数改变事件
    sizeChange(pageSize) {
      this.pageInfo.pageSize = pageSize;
      this.fetchData()
    }
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

p {
  margin: 0;
  padding: 0;
}

#clock {
  font-family: 'Share Tech Mono', monospace;
  color: #ffffff;
  text-align: center;
  color: #3F9EFF;
  text-shadow: 0 0 3px yellow;

  .time {
    letter-spacing: 0.05em;
    font-size: 20px;
    padding: 5px 0;
  }

  .date {
    letter-spacing: 0.1em;
    font-size: 18px;
  }

  .text {
    letter-spacing: 0.1em;
    font-size: 12px;
    padding: 20px 0 0;
  }
}
</style>